<template>
  <basic-container>
    <avue-crud
      ref="crud"
      v-model="form"
      :page.sync="page"
      :data="tableData"
      :table-loading="tableLoading"
      :option="tableOption"
      :before-open="beforeOpen"
      @on-load="getList"
      @search-change="searchChange"
      @search-reset="searchReset"
    >
      <template v-slot:userInfo="{ row }">
        <div class="y-center">
          <el-avatar
            :src="row.avatar"
            :size="18"
          ></el-avatar>
          {{ row.nickName }}
        </div>
      </template>
      <template v-slot:pet="{ row }">
        <div class="y-center">
          <el-avatar
            :src="row.petAvatar"
            :size="18"
          ></el-avatar>
          {{ row.petName }}
        </div>
      </template>
      <template v-slot:petCode="{ row }">
        <el-button
          type="text"
          @click="showPetDetail(row.petId)"
        >
          {{ row.petCode }}
        </el-button>
      </template>
      <template v-slot:menu="{ size, row, index }">
        <el-button
          @click="showCannedDetail(row, index)"
          type="text"
          :size="size"
        >
          收支明细
        </el-button>
      </template>
    </avue-crud>
    <detailPopup
        :id="petId"
        v-model="petDetailVisible"
    ></detailPopup>
    <cannedDetailPopup
      v-if="rowId"
      :id="rowId"
      v-model="cannedDetailVisible"
    ></cannedDetailPopup>
  </basic-container>
</template>

<script>
import { fetchList } from '@/api/core/canned';
import { tableOption } from '@/const/crud/points/canned';
import detailPopup from '@/views/customer/pet-file/components/detailPopup.vue';
import cannedDetailPopup from '@/views/points/canned/components/cannedDetailPopup.vue';
import { mapGetters } from 'vuex';
import crudMixins from '@/mixins/crud';

export default {
  name: 'cannedManage',
  components: { detailPopup, cannedDetailPopup },
  mixins: [crudMixins],
  data() {
    return {
      editVisible: false,
      form: {},
      tableOption: tableOption(),
      userId: null,
      petId: null,
      petDetailVisible: false,
      addVisible: false,
      rowId: null,
      cannedDetailVisible: false,
    };
  },
  computed: {
    ...mapGetters(['permissions']),
  },
  watch: {},
  methods: {
    edit(row) {
      this.editVisible = true;
      this.userId = row.userId;
    },
    add() {
      this.addVisible = true;
    },
    // 列表查询
    getList(page, refresh) {
      if (refresh) {
        this.page.current = 1;
        this.page.currentPage = 1;
      }
      this.tableLoading = true;
      fetchList(
        Object.assign(
          {
            current: this.page.currentPage,
            size: this.page.pageSize,
          },
          this.searchForm
        )
      )
        .then((response) => {
          this.tableData = response.data.data.records;
          this.page.total = response.data.data.total;
          this.tableLoading = false;
        })
        .catch(() => {
          this.tableLoading = false;
        });
    },
    // 打开前的拦截
    async beforeOpen(done, type) {
      console.log('beforeOpen', type);
      if (type !== 'add') {
        const res = await getObj(this.form.id);
        this.form = { ...res.data.data };
      }
      done();
    },

    showPetDetail(id) {
      this.petId = id;
      this.petDetailVisible = true;
    },
    showCannedDetail(row, index) {
      this.rowId = row.id;
      this.cannedDetailVisible = true;
    },
  },
};
</script>
